package jdk.thread.newThead;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

public class CompletableFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(CompletableFutureTest::calculate);


        future.thenAccept((result)->{
            System.out.println("计算完成，返回结果："+result);
        });

        future.exceptionally((e) -> {
            System.out.println("计算异常，返回默认结果");
            return null;
        });

        System.out.println("最终返回结果："+future.get());
    }

    static Integer calculate() {
        System.out.println("开始计算。。。");
        try {
            Thread.sleep(1000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        throw new RuntimeException();
//        return 102324;
    }
}
